package cc.owoo.godpen.analysis.json;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * 当值为键值对时，能够进行的操作
 * Created by nimensei
 * 2022-05-16 上午 10:41
 **/
public final class JsonMap extends Json {
    private Map<Object, Object> map;

    JsonMap(Map<Object, Object> map) {
        this.map = map;
    }

    @Override
    public Object getData() {
        return map;
    }

    @Override
    boolean setData(Object data) {
        if (!(data instanceof Map<?, ?> map))
            return false;
        this.map = (Map<Object, Object>) map;
        return true;
    }

    @Override
    boolean setValue(String key, Object value) {
        try {
            map.put(key, value);
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    @Override
    boolean setValue(int index, Object value) {
        try {
            map.put(index, value);
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    @Override
    Object getValue(String key) {
        try {
            return map.get(key);
        } catch (Exception e) {
            return null;
        }
    }

    @Override
    Object getValue(int index) {
        try {
            return map.get(String.valueOf(index));
        } catch (Exception e) {
            return null;
        }
    }

    @Override
    public Object removeValue(String key) {
        return null;
    }

    @Override
    public Object removeValue(int index) {
        return null;
    }

    @Override
    Boolean getBooleanValue() {
        return null;
    }

    @Override
    Number getNumberValue() {
        return null;
    }

    @Override
    CharSequence getCharSequenceValue() {
        return null;
    }

    @Override
    public HashMap<String, Object> getHashMap() {
        HashMap<String, Object> hashMap = new HashMap<>();
        for (var entry : map.entrySet())
            if (entry.getKey() != null)
                hashMap.put(entry.getKey().toString(), entry.getValue());
        return hashMap;
    }

    @Override
    public ArrayList<Object> getArrayList() {
        return null;
    }
}
